<?php


namespace app\zftpay\controller;


use app\BaseController;
use app\service\alipayWapService;
use app\service\curlService;
use app\service\propertyService;

use app\service\RedisQueueService;
use app\service\serviceService;
use app\service\settleService;
use app\service\zftService;
use app\zftpay\service\apiService;


use think\facade\Db;
use think\facade\Log;
use think\Queue;

class Callback extends BaseController
{
    public function getuser(){
        $api = new alipayWapService();
        //获取当前用户支付宝ID
        $code = request()->get('auth_code');
        $oauthtoken = $api->oauthToken($code);

        if(property_exists($oauthtoken,'sub_msg')){
            return json(['info' => $oauthtoken->sub_msg]);
        }

        //Log::write($oauthtoken,"oauthtoken");
        session('user_id',$oauthtoken->user_id);
        $url=session("url");
        $this->redirect($url);
        die;
    }

    public function notice(){
        $path=dir(getcwd())->path.'/log/pay/zft/'.date('Ymd');
        if (! file_exists ( $path )) {
            mkdir ( "$path", 0777, true );
        }
        $address = $path."/".date('His').rand(100000,999999).'.txt';
        file_put_contents($address,file_get_contents("php://input"),FILE_APPEND);
        parse_str(file_get_contents($address),$result);//读取文本并变成数组
        $notify_con=file_get_contents($address);
        if($result['trade_status'] == 'TRADE_SUCCESS'){
            $billinfo=Db::table("zft_order")->where('trade_no',$result['trade_no'])->find();
            if(!empty($billinfo)){
                //查询是否有
                $orderInfo=Db::table("zft_order_success")
                    ->where('trade_no',$result['trade_no'])->find();
                if(empty($orderInfo)){
                    $res=Db::table("zft_order")->where("id",$billinfo["id"])->update(["order_status"=>"SUCCESS","pay_type"=>"1","pay_time"=>$result['gmt_payment']]);
                    $billinfo["order_status"]="SUCCESS";
                    $billinfo["pay_type"]="1";
                    $billinfo["alipay_notify_con"]=$notify_con;
                    $billinfo["pay_time"]=$result['gmt_payment'];
                    $billinfo["create_time"]=date("Y-m-d H:i:s");
                    $id=Db::table("zft_order_success")->insertGetId($billinfo);
                     //$queueName  	  = "app\job\ZftNotifyUrl";
                     //$jobData       	  = [ 'order_uid' =>$id ,"alipay_notify_con"=>$notify_con,"community_id"=>$billinfo["community_id"]] ;
                     //$resu= \think\facade\Queue::push($queueName,$jobData);

                     $this->doNotifyJob($id,$notify_con,$billinfo["community_id"]);

                    $jobQueueName  	  = "app\job\ZftSettleConfirmOrder";
                    $jobData       	  = [ 'id' =>$id ] ;
                    (new settleService())->settleConfirmOrder($jobData);
                    return "success";
                    //$res= \think\facade\Queue::push($jobQueueName,$jobData);
                }

            }
        }
    }
    public function wapNotice(){
        $path=dir(getcwd())->path.'/log/pay/zft/'.date('Ymd');
        if (! file_exists ( $path )) {
            mkdir ( "$path", 0777, true );
        }
        $address = $path."/".date('His').rand(100000,999999).'.txt';
        file_put_contents($address,file_get_contents("php://input"),FILE_APPEND);
        parse_str(file_get_contents($address),$result);//读取文本并变成数组
        $notify_con=file_get_contents($address);
        if($result['trade_status'] == 'TRADE_SUCCESS'){
            $billinfo=Db::table("zft_order")->where('out_trade_no',$result['out_trade_no'])->find();
            if(!empty($billinfo)){
                //查询是否有
                $orderInfo=Db::table("zft_order_success")
                    ->where('out_trade_no',$result['out_trade_no'])->find();
                if(empty($orderInfo)){

                    $user_id="";
                    if (array_key_exists('buyer_open_id', $result)) {
                        $user_id=$result["buyer_open_id"];
                    }
                    if (array_key_exists('buyer_id', $result)) {
                        $user_id=$result["buyer_id"];
                    }
                    $res=Db::table("zft_order")
                        ->where("id",$billinfo["id"])
                        ->update(["order_status"=>"SUCCESS","trade_no"=>$result['trade_no'],"user_id"=>$user_id,"pay_type"=>"1","pay_time"=>$result['gmt_payment']]);
                    $billinfo["order_status"]="SUCCESS";
                    $billinfo["pay_type"]="1";
                    $billinfo["alipay_notify_con"]=$notify_con;
                    $billinfo["pay_time"]=$result['gmt_payment'];
                    $billinfo["trade_no"]=$result['trade_no'];
                    $billinfo["user_id"]=$user_id;
                    $billinfo["create_time"]=date("Y-m-d H:i:s");
                    $id=Db::table("zft_order_success")->insertGetId($billinfo);
                    //$queueName  	  = "app\job\ZftNotifyUrl";
                    //$jobData       	  = [ 'order_uid' =>$id ,"alipay_notify_con"=>$notify_con,"community_id"=>$billinfo["community_id"]] ;
                    //$resu= \think\facade\Queue::push($queueName,$jobData);

                    $this->doNotifyJob($id,$notify_con,$billinfo["community_id"]);

                    $jobQueueName  	  = "app\job\ZftSettleConfirmOrder";
                    $jobData       	  = [ 'id' =>$id ] ;
                    (new settleService())->settleConfirmOrder($jobData);
                    return "success";
                    //$res= \think\facade\Queue::push($jobQueueName,$jobData);
                }

            }
        }
    }
    public function wapNoticeTest(){
        $path=dir(getcwd())->path.'/log/pay/zft/'.date('Ymd');
        if (! file_exists ( $path )) {
            mkdir ( "$path", 0777, true );
        }
        $address = $path."/200139231639.txt";
        //file_put_contents($address,file_get_contents("php://input"),FILE_APPEND);
        parse_str(file_get_contents($address),$result);//读取文本并变成数组
        $notify_con=file_get_contents($address);
        if($result['trade_status'] == 'TRADE_SUCCESS'){
            $billinfo=Db::table("zft_order")->where('out_trade_no',$result['out_trade_no'])->find();
            if(!empty($billinfo)){
                //查询是否有
                $orderInfo=Db::table("zft_order_success")
                    ->where('out_trade_no',$result['out_trade_no'])->find();
                if(empty($orderInfo)){

                    $user_id="";
                    if (array_key_exists('buyer_open_id', $result)) {
                        $user_id=$result["buyer_open_id"];
                    }
                    if (array_key_exists('buyer_id', $result)) {
                        $user_id=$result["buyer_id"];
                    }
                    $res=Db::table("zft_order")
                        ->where("id",$billinfo["id"])
                        ->update(["order_status"=>"SUCCESS","trade_no"=>$result['trade_no'],"user_id"=>$user_id,"pay_type"=>"1","pay_time"=>$result['gmt_payment']]);
                    $billinfo["order_status"]="SUCCESS";
                    $billinfo["pay_type"]="1";
                    $billinfo["alipay_notify_con"]=$notify_con;
                    $billinfo["pay_time"]=$result['gmt_payment'];
                    $billinfo["trade_no"]=$result['trade_no'];
                    $billinfo["user_id"]=$user_id;
                    $billinfo["create_time"]=date("Y-m-d H:i:s");
                    $id=Db::table("zft_order_success")->insertGetId($billinfo);
                    //$queueName  	  = "app\job\ZftNotifyUrl";
                    //$jobData       	  = [ 'order_uid' =>$id ,"alipay_notify_con"=>$notify_con,"community_id"=>$billinfo["community_id"]] ;
                    //$resu= \think\facade\Queue::push($queueName,$jobData);

                    $this->doNotifyJob($id,$notify_con,$billinfo["community_id"]);

                    $jobQueueName  	  = "app\job\ZftSettleConfirmOrder";
                    $jobData       	  = [ 'id' =>$id ] ;
                    (new settleService())->settleConfirmOrder($jobData);
                    return "success";
                    //$res= \think\facade\Queue::push($jobQueueName,$jobData);
                }

            }
        }
    }
    public function doNotifyJob($order_uid,$notify_con,$community_id){
        $url=(new serviceService())->getNotifyUrlByComId($community_id);
        $res=(new curlService())->curl_post($url,$notify_con,[]);
        Log::write($res,"doNotifyJob");
        print("<info>ZftNotifyUrl Job doJob. job Data is: ".var_export($res,true)."</info> \n");
        Db::table("zft_order_success")
            ->where("uid",$order_uid)
            ->update(["notify_time"=>date('Y-m-d H:i:s')]);
        if($res=="success"){
            return true;
        }else{
            $queueName  	  = "app\job\ZftNotifyUrl";
            $jobData       	  = [ 'order_uid' =>$order_uid,"alipay_notify_con"=>$notify_con,"community_id"=>$community_id] ;
            $resu= \think\facade\Queue::push($queueName,$jobData);
            return false;
        }

    }


}